/**
 *
 */
package ua.at.tsvetkov.samizdat.activities;

import ua.at.tsvetkov.util.Log;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import ua.at.tsvetkov.samizdat.Const;
import ua.at.tsvetkov.samizdat.adapters.AdtGenres;
import ua.at.tsvetkov.samizdat.data.Data;
import ua.at.tsvetkov.samizdat.data.DataDownloader;

/**
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
 */
public class AcGenres extends AcTemplate {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		addListView();
		dataDownloader = new DataDownloader(this, (ProgressDialog) onCreateDialog(DIALOG_LOAD), Const.URL_GENRES, Const.LIST_VIEW_COUNT);
		dataDownloader.setOnCompleteLoadListener(standartNotifier);
		dataDownloader.download(35, Const.FILE_GENRES, false, false);
		startPosition = 0;
		adapter = new AdtGenres(this, dataDownloader, Const.LIST_VIEW_COUNT);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (showMore(position, listView))
					return;
				String[] genreData = dataDownloader.getDataFields(position - 1, Data.GENRE_FIELDS_COUNT);
				Log.i("Selected " + genreData[Data.GENRE_TEXT]);
				Intent intent = new Intent(AcGenres.this, AcGenreBooks.class);
				intent.putExtra("code", genreData[Data.GENRE_CODE]);
				intent.putExtra("count", genreData[Data.GENRE_COUNT]);
				intent.putExtra("genre", genreData[Data.GENRE_TEXT]);
				startActivity(intent);
			}

		});
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		adapter.notifyDataSetChanged();
		super.onResume();
	}

}
